package com.krtech.wecard.module.sys.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import com.krtech.wecard.module.sys.entity.StatisticalMain;
import com.krtech.wecard.module.sys.mapper.StatisticalMainMapper;
import com.krtech.wecard.module.sys.service.StatisticalMainService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StatisticalMainServiceImpl extends ServiceImpl<StatisticalMainMapper, StatisticalMain> implements StatisticalMainService {

    @Autowired
    private StatisticalMainMapper statisticalMainMapper;


    @Override
    public int updateBatch(List<StatisticalMain> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<StatisticalMain> list) {
        return baseMapper.batchInsert(list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addBuriedPoint(StatisticalMain statisticalMain) {
        //通过用户id（code），模块名，日期，查出当日这个模块模块店庆
        SysUser userInfo = LoginUtil.getUserInfo();

        DateTime time = DateUtil.parse(DateUtil.formatDate(new Date()), "yyyy-MM-dd");

        List<StatisticalMain> statisticalMainList = this.list(new QueryWrapper<StatisticalMain>()
                .eq(StatisticalMain.COL_MODULE_NAME, statisticalMain.getModuleName())
                .eq(StatisticalMain.COL_USER_ID, userInfo.getUserId())
                .eq(StatisticalMain.COL_ACCESS_TIME, time));

        if (statisticalMainList.size() > 1){
            throw new BusinessException(userInfo.getNickName()  +","+statisticalMain.getModuleName()+","+time+",埋点数量异常");
        }

        if (statisticalMainList.size() > 0){
            //有，就用现在这个
            StatisticalMain currentMain = statisticalMainList.get(0);

            int increment = statisticalMainMapper.increment(currentMain.getId());
            if (increment <= 0 ){
                throw new BusinessException("埋点增加异常");
            }

        }else {
            //没有，就新建
            statisticalMain.setCreatedBy(userInfo.getUserId().toString());
            statisticalMain.setCreatedTime(new Date());
            statisticalMain.setUserId(userInfo.getUserId().intValue());
            statisticalMain.setUserName(userInfo.getUserName());
            statisticalMain.setUserCode(userInfo.getUserCode());


            statisticalMain.setAccessTime(time);
            statisticalMain.setCount(1L);

            this.save(statisticalMain);
        }

        return true;
    }
}


